package com.wswy.wzcx.widget

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.Log
import android.widget.FrameLayout
import android.graphics.PorterDuff
import android.graphics.PorterDuffXfermode
import android.os.Build
import android.support.annotation.DrawableRes
import android.support.v4.content.ContextCompat
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.SizeUtils
import com.wswy.wzcx.R
import com.wswy.wzcx.widget.drawable.GuideArrowDrawable
import kotlinx.android.synthetic.main.layout_newbie_info.view.*


class NewbieGuideLayout : FrameLayout ,View.OnClickListener{


    companion object {
        private const val TAG = "NewbieGuideLayout"
    }

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attribute: AttributeSet?) : this(context, attribute, 0)

    constructor(context: Context, attribute: AttributeSet?, defStyle: Int) : super(context, attribute, defStyle) {

        setWillNotDraw(false)

        setLayerType(LAYER_TYPE_SOFTWARE, null);



        setOnClickListener(this)

    }

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)


    private val flBg = GuideArrowDrawable()
    private var step = 0

    private lateinit var imgIcon:ImageView
    private lateinit var textView: TextView
    private lateinit var flBgView:View

    var stepEndCallback : ((View)->Unit?)?=null

    init {

//        LinearLayout(context).apply {
//            if (Build.VERSION.SDK_INT<= Build.VERSION_CODES.JELLY_BEAN_MR1){
//                setLayerType(View.LAYER_TYPE_SOFTWARE,null)
//            }
//            orientation = LinearLayout.HORIZONTAL
//            gravity = Gravity.CENTER_VERTICAL
//            background = flBg
//
//            imgIcon = ImageView(context)
//
//            addView(imgIcon,LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT).apply {
//                leftMargin = SizeUtils.dp2px(8f)
//            })
//
//            textView = TextView(context)
//            textView.textSize = 18f
//            textView.setTextColor(Color.WHITE)
//            addView(textView,LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT).apply {
//                leftMargin = SizeUtils.dp2px(8f)
//            })
//
//        }.let {
//
//            val lp = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,SizeUtils.dp2px(70f))
//
//            lp.gravity = Gravity.BOTTOM
//            lp.bottomMargin = SizeUtils.dp2px(55f)
//
//            addView(it,lp)
//        }


        View.inflate(context, R.layout.layout_newbie_info,this)

        imgIcon = findViewById(R.id.img_hint)
        textView = findViewById(R.id.tv_hint)
        flBgView = findViewById<View>(R.id.view_bg)

    }


    private val guideItems = arrayListOf<GuideItem>()

    private var currGuideItem : GuideItem?=null


    val xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR)

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        canvas?.run {

            paint.xfermode = xfermode

            val item = currGuideItem?:return

            if (!item.rect.isEmpty){

                Log.e(TAG, " --> draw --->>> "+item.rect.width()+"   "+item.rect.height())

                when(item.maskType){
                    GuideItem.CIRCLE -> {
                        drawCircle(item.rect.centerX(),item.rect.centerY(),item.rect.height()/2,paint)
                    }

                    GuideItem.ROUND_RECT -> {
                        drawRoundRect(item.rect,item.rect.height()/2,item.rect.height()/2,paint)
                    }
                }


            }

        }

    }

    fun setGuideItem(items:List<GuideItem>,bgColor:Int){
        setBackgroundColor(bgColor)

        step = 0

        guideItems.clear()
        guideItems.addAll(items)

        movePos()
        invalidate()
    }

    fun setGuideItem(items:List<GuideItem>){
        setGuideItem(items,ContextCompat.getColor(context,R.color.dimColor))
    }


    override fun onClick(v: View?) {

        step++
        movePos()
        if (step >=guideItems.size){
            stepEndCallback?.invoke(this)
        }
    }


    private fun movePos(){
        guideItems.getOrNull(step)?.run {

            Log.e(TAG, " -->$this ");

            currGuideItem = this

            imgIcon.setImageResource(icon)
            textView.text = hint

            markAnchor(rect)

            flBgView.background = flBg

            val arrowUp = ScreenUtils.getScreenHeight()/2 > rect.bottom
            flBg.setArrowAnchor(rect,arrowUp,startColor, endColor)

        }
    }

    private fun markAnchor(rectF:RectF){
        val screenHeight = ScreenUtils.getScreenHeight()

        (guide_layout.layoutParams as? FrameLayout.LayoutParams)?.run {


            if (screenHeight/2 > rectF.bottom){
                gravity = Gravity.TOP
                topMargin = SizeUtils.dp2px(40f)
                //topMargin = (rectF.bottom + SizeUtils.dp2px(10f)).toInt()
            }else{
                gravity = Gravity.BOTTOM
                bottomMargin = SizeUtils.dp2px(55f)
            }


            guide_layout.layoutParams = this
        }

    }


    data class GuideItem(val rect: RectF,
                         @DrawableRes val icon:Int,
                         val hint:String,
                         val maskType : Int = CIRCLE,
                         val startColor:Int,
                         val endColor:Int

                         ){


        companion object {
            val NONE = -1
            val CIRCLE = 0
            val ROUND_RECT = 1
        }

    }


}